// 封装ajax请求
// axios
import axios, { AxiosRequestConfig, AxiosResponse } from 'axios';

const service = axios.create({
  baseURL: 'http://localhost:4002',
  timeout: 5000,
  headers: {
    'Content-Type': 'application/json',
  },
});

service.interceptors.request.use(
  (config: AxiosRequestConfig) => {
    config.headers!.Authorization = `Bearer ${localStorage.getItem('token')}`;
    return config;
  },
  (error: any) => {
    return Promise.reject(error);
  },
);

service.interceptors.response.use(
  (response: AxiosResponse) => {
    // .data 省略了
    return response.data;
  },
  (error: any) => {
    return Promise.reject(error);
  },
);

export default service;
